/*
 * Verify.java
 * 
 * Version information
 * 
 * Mar 27, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.icontact.webapp.struts.action;

import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import static net.amsoft.iservice.icontact.util.IContactConstants.*;
import net.amsoft.iservice.icontact.hibernate.Message;
import net.amsoft.iservice.icontact.service.IContact;
import net.amsoft.iservice.icontact.util.IContactConstants;
import net.amsoft.iservice.util.IServiceUtil;

/**
 * Verify class comes into action when a sender clicks the verification link
 * sent by the application, hence application comes ot know that e-mail provided
 * by the sender is valid and the process of placing a contact request is
 * completed.
 * 
 * @author rahul
 * 
 */
public class Verify extends BaseAction {

	private static final Logger oLogger = Logger.getLogger(Verify.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process() : entry");

		ActionMessages oMsgs = new ActionErrors();
		try {
			String sKey = oRequest.getQueryString();
			if (sKey == null) {
				return oMapping.findForward(FWD_SUCCESS);
			}
			String sEncodedKey = URLDecoder.decode(sKey.trim(), "UTF-8");
			String sHash = IServiceUtil.getOneWayEncValue(sEncodedKey);
			oLogger.debug("process() : ALL HASHs: " + sKey + ", " + sEncodedKey
					+ ", " + sHash);

			Message oMessage = IContact.updateMessageState(sHash,
					IContactConstants.PENDING, ACTIVE);
			sendNotificationEmail(oMessage, oRequest);
			oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					MSG_REQ_SENT));
			saveErrors(oRequest, oMsgs);
			oLogger.info("process() : Message activated");
			return oMapping.findForward(FWD_MSG);
		} catch (Exception e) {
			oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ERR_INVALID, "request"));
			saveErrors(oRequest, oMsgs);
			oLogger.warn("process() : Exception : " + e.getMessage());
			ActionForward oFwd = oMapping.findForward(FWD_MSG);
			return oFwd;
		}
	}
}
